package com.ebelter.btcomlib.utils;


/**
 * Created by ZCQ on 2017/11/9
 */

public class BytesUtils {

    //private static final String TAG = "BytesUtils";
    /**
     * byte数组转换成16进制的字符串
     * <li>{2,17,22,127}---->[02 11 16 7F]<li/>*/
    public static String bytes2HexStr(byte[] data) {

        if (data==null||data.length==0){
            return "[ ]";
        }
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        for (byte b : data) {
            builder.append(String.format("%02X",b)).append(" ");
        }
        builder.deleteCharAt(builder.lastIndexOf(" "));
        builder.append("]");
        return builder.toString();
    }

    public static String bytes2HexStr2(byte[] data) {
        if (data==null||data.length==0){
            return "";
        }
        StringBuilder builder = new StringBuilder();
        for (byte b : data) {
            builder.append(String.format("%02X",b));
        }
        return builder.toString();
    }







    /**高低两字节组成int*/
    public static int getIntFrom2Byte(byte h,byte l) {
        return ((l & 0xff)) | ((h & 0xff) << 8);
    }


    /** 高低两字节组成新的byte */
    public static int getByteFrom2Byte(byte h, byte l) {
        return ((h & 0x0f) << 4) | ((l & 0x0f));
    }

    public static byte[] getHLint(int value) {
        byte[] byteArray = new byte[2];
        byteArray[1] = (byte) value;//低字节
        byteArray[0] = (byte) (value >> 8);//高字节
        return byteArray;
    }

    public static byte[] getHLByte(byte value) {
        byte[] byteArray = new byte[2];
        byteArray[1] = (byte) (value& 0x0f);//低字节
        byteArray[0] = (byte) (value >> 4);//高字节
        return byteArray;
    }



    /**
     * 计算某个字节的第几位是0还是1 也即是高低位
     * 例如 3（0000 0011）的第6位是1 即高位 从左右的
     * 例如 3（0000 0011）的第0位是0 即低位
     */
    public static int getByteHLByIndex(byte data,int index){
        int result;
        if (index==0){
            result=((data&0x80)>>7);
        }else if (index==1){
            result=((data&0x40)>>6);
        }else if (index==2){
            result=((data&0x20)>>5);
        }else if (index==3){
            result=((data&0x10)>>4);
        }else if (index==4){
            result=((data&0x08)>>3);
        }else if (index==5){
            result=((data&0x04)>>2);
        }else if (index==6){
            result=((data&0x02)>>1);
        }else if (index==7){
            result=((data&0x01));
        }else {
            throw new RuntimeException("运行时异常：data = "+data+"--index = "+index);
        }
        return result;
    }


    public static byte[] copyBytes(byte[] bytes){
        byte[] bs = new byte[bytes.length];
        System.arraycopy(bytes,0,bs,0,bytes.length);
        return bs;
    }


    // msgBytes[5] = (byte) (time & 0xff);//0-7Bit
    // msgBytes[6] = (byte) ((time >> 8) & 0xff);//8-15Bit
    // msgBytes[7] = (byte) ((time >> 16) & 0xff);//16-23Bit
    // msgBytes[8] = (byte) ((time >> 24) & 0xff);//24-32Bit

    public static byte[] getBytesFromLong(long value) {
        byte[] result = new byte[4];
        result[0] = (byte) (value & 0xff);// 0-7Bit
        result[1] = (byte) ((value >> 8) & 0xff);// 8-15Bit
        result[2] = (byte) ((value >> 16) & 0xff);// 16-23Bit
        result[3] = (byte) ((value >> 24) & 0xff);// 24-32Bit
        return result;
    }

    public static long getLongFromBytes(byte[] bytes) {
        long result = ((bytes[3] & 0xff) << 24) | ((bytes[2] & 0xff) << 16) | ((bytes[1] & 0xff) << 8)
                | (bytes[0] & 0xff);
        return result;
    }



}
